define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'saler/saler/index',
                    add_url: 'saler/saler/add',
                    edit_url: 'saler/saler/edit',
                    del_url: 'saler/saler/del',
                    multi_url: 'saler/saler/multi',
                    import_url: 'saler/saler/import',
                    table: 'saler',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 20,
                pageList: [20, 50],
                clickToSelect: false,
                dblClickToEdit: false,
                singleSelect: false,
                columns: [
                    [{
                            checkbox: true,
                            visible: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        // {field: 'admin_id', title: __('Admin_id'), visible: false},
                        {
                            field: 'name',
                            title: __('Name')
                        },
                        {
                            field: 'tel',
                            title: __('Tel')
                        },
                        {
                            field: 'company',
                            title: __('公司名称')
                        },
                        // {
                        //     field: 'createtime',
                        //     title: __('Createtime'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     formatter: Table.api.formatter.datetime
                        // },
                        // {
                        //     visible: false,
                        //     field: 'updatetime',
                        //     title: __('Updatetime'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     formatter: Table.api.formatter.datetime
                        // },
                        // {
                        //     field: 'admin.id',
                        //     title: __('Admin.id'),
                        //     visible: false
                        // },
                        {
                            field: 'white',
                            title: __('白名单'),
                            table: table,
                            formatter: function (value, row, index) {
                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                var buttons = [];
                                if (value == 0) {
                                    buttons.push({
                                        name: 'start_task',
                                        icon: 'fa fa-circle-o',
                                        title: __('加入白名单'),
                                        extend: 'data-toggle="tooltip"',
                                        classname: 'btn-add-white',
                                        url: options.extend.start_task_url
                                    });
                                } else if (value == 1) {
                                    buttons.push({
                                        name: 'start_task',
                                        icon: 'fa fa-check-circle-o',
                                        title: __('移出白名单'),
                                        extend: 'data-toggle="tooltip"',
                                        classname: 'btn-add-white',
                                        url: options.extend.start_task_url
                                    });
                                }
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                                return value;
                            },
                            events: {
                                'click .btn-add-white': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var that = this;
                                    var $that = $(this);
                                    var top = $(that).offset().top - $(window).scrollTop();
                                    var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                    if (top + 154 > $(window).height()) {
                                        top = top - 154;
                                    }
                                    if ($(window).width() < 480) {
                                        top = left = undefined;
                                    }
                                    if ($(this).find('i').hasClass('fa-circle-o')) {
                                        Layer.confirm(
                                            __('确定加入白名单?'), {
                                                icon: 3,
                                                title: __('Warning'),
                                                offset: [top, left],
                                                shadeClose: true
                                            },
                                            function (index) {
                                                $.post("/admin/saler/saler/white/id/" + row.id + "/white/1", {
                                                    id: row.id
                                                }, function (data) {
                                                    Layer.close(index);
                                                    if (data == "1") {
                                                        $that.attr('data-original-title', '移出白名单')
                                                        $that.find('i').addClass('fa-check-circle-o').removeClass('fa-circle-o');
                                                        Toastr.success("成功加入白名单");
                                                        table.bootstrapTable('refresh');
                                                    } else {
                                                        Toastr.error(data.msg);
                                                    }
                                                });
                                            }
                                        );
                                    } else {
                                        Layer.confirm(
                                            __('确定移出白名单?'), {
                                                icon: 3,
                                                title: __('Warning'),
                                                offset: [top, left],
                                                shadeClose: true
                                            },
                                            function (index) {
                                                $.post("/admin/saler/saler/white/id/" + row.id + "/white/0", {
                                                    id: row.id
                                                }, function (data) {
                                                    Layer.close(index);
                                                    if (data == "1") {
                                                        $that.attr('data-original-title', '加入白名单')
                                                        $that.find('i').addClass('fa-circle-o').removeClass('fa-check-circle-o');
                                                        Toastr.success("成功移出白名单");
                                                        table.bootstrapTable('refresh');
                                                    } else {
                                                        Toastr.error(data.msg);
                                                    }
                                                });
                                            }
                                        );
                                    }
                                }
                            }
                        },
                        {
                            field: 'admin.nickname',
                            title: __('销售代表')
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('.btn-singlesearch').on('click', function () {
                $('.form-control[name="white"]').val("")
                $('.btn-info[type="submit"]').trigger('click');
                Toastr.success("全部名单搜索成功");
            });
            $('.btn-singlesearcha').on('click', function () {
                $('.form-control[name="white"]').val("1")
                $('.btn-info[type="submit"]').trigger('click');
                Toastr.success("白名单搜索成功");
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});